﻿package com.lmm;


import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ibm.aglet.Aglet;
import com.ibm.aglet.AgletException;
import com.ibm.aglet.Message;
import com.ibm.agletx.util.SimpleItinerary;

public class CollectAgent extends Aglet {

	/**
	 * 串行化
	 */
	private static final long serialVersionUID = -2736831796946922269L;
	//发送代理
	private SimpleItinerary itinerary = null;
	//主机地址
	private String homeUrl;
	// 路由信息
	private  ExchangeRoadModel exchangeModel = new ExchangeRoadModel();	
	/**
	 * 系统创建
	 */
	public void onCreation(Object init) {
		System.out.println(AgentConfig.V_STR_CRLF+"-------------The host is running and creates an agent!-------------");
		itinerary = new SimpleItinerary(this);
		homeUrl = getAgletContext().getHostingURL().toString();
		exchangeModel.setVisitWayList(createRoadList());
		exchangeModel.resetArriveWay();		
		String nextPath = exchangeModel.getWay();
		if (null == nextPath)
		{
			System.out.println("All Way is Running!");
			return;
		}		
		sendRemoteAgent(nextPath , new Message(AgentConfig.V_CMD_COLLECT_DATA, exchangeModel));		
	}

	// 收到消息后进行处理
	@Override
	public boolean handleMessage(Message msg)
	{
		// 如果是发送的数据命令，则打印数据		
			if (msg.sameKind(AgentConfig.V_CMD_COLLECT_DATA))
			{	
				exchangeModel = (ExchangeRoadModel) msg.getArg();
				String nextPath = "";
				exchangeModel.putData(collectData());
				if(exchangeModel.getVisitWaySize()>0)
				{
					nextPath= exchangeModel.getWay();
					exchangeModel.setCourrentPath(nextPath);
					setText("will go to " +nextPath);  //在tahiti控制台显示提示信息
					sendRemoteAgent(nextPath , new Message(AgentConfig.V_CMD_COLLECT_DATA,exchangeModel));	
				}
				else
				{	
					setText("will go back to "+homeUrl);
					sendRemoteAgent(homeUrl, new Message(AgentConfig.V_CMD_COLLECT_RETRACTCOLLECT,exchangeModel));
				}
			}
			else if(msg.sameKind(AgentConfig.V_CMD_COLLECT_RETRACTCOLLECT))
			{	
				exchangeModel = (ExchangeRoadModel) msg.getArg();
				exchangeModel.putData(collectData());
				System.out.println(AgentConfig.V_STR_SPLITCHAR);
				System.out.println("The data collected for this time is:");
				System.out.println(exchangeModel.getData());
				exchangeModel.printRoad(homeUrl);
				waitMessage(20 * 1000); // show message, 2 seconds
				dispose(); 
			}
			else
			{
				return false;
			}
		
		return true;
	}

	private void sendRemoteAgent(String url, Message msg)  {
		try {
			waitMessage(10 * 1000); // show message, 2 seconds
			itinerary.go(url, msg);			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (AgletException e) {
			e.printStackTrace();
		}
	}
	private List<Object> createRoadList()
	{
		List<Object> list= new ArrayList<Object>();		
		list.add("atp://lmm:5100/");
		list.add("atp://lmm:5200/");
		list.add("atp://lmm:5300/");
		list.add("atp://lmm:5400/");
		list.add("atp://lmm:5500/");
		return list;
	}
	/**
	 * 采集 数据方法
	 * 
	 * @return 采集结果
	 */
	private String collectData()
	{
		// 获取系统IP地址
		String ipAddress = getAgletContext().getHostingURL().toString();
		String userName = AgentConfig.V_DEFAULT_USERNAME;
		try
		{
			InetAddress inet = InetAddress.getLocalHost();
			userName = inet.getHostName();
		}
		catch (UnknownHostException e)
		{
			e.printStackTrace();
		}
		SimpleDateFormat sdf = new SimpleDateFormat(AgentConfig.V_FMT_DATE_23);
		StringBuilder strbuf = new StringBuilder();
		strbuf.append("IP:" + ipAddress + AgentConfig.V_STR_SPACE);
		strbuf.append("UserName:" + userName + AgentConfig.V_STR_SPACE);
		strbuf.append("CollectTime:" + sdf.format(new Date())+AgentConfig.V_STR_CRLF);
		System.out.println(AgentConfig.V_STR_CRLF+"------Data collection starts" );
		System.out.println(strbuf.toString()+"------Data collection ends");			
		return strbuf.toString();
	}
}
